import numpy as np
import pandas as pd
from pandas import Series, DataFrame

t1 = pd.date_range( "2016-01-01", "2016-12-31" )

s1 = Series( np.random.randn( len( t1 ) ), index = t1 )

# 按照每个月进行采样，并且取得每一个月的平均值
s1_month = s1.resample( "M" ).mean()
# print( s1_month )
''' 按照每个月进行采样，并且取得每一个月的平均值
2016-01-31    0.219179
2016-02-29   -0.099159
2016-03-31    0.130241
2016-04-30    0.154243
2016-05-31   -0.050391
2016-06-30    0.153276
2016-07-31    0.102570
2016-08-31   -0.157332
2016-09-30    0.101782
2016-10-31    0.005016
2016-11-30   -0.179196
2016-12-31   -0.260828
Freq: M, dtype: float64
'''

# 现在索引是按天计的，现在需要按照时计，这就会造成数据不足，因此需要填充
s1_resample_a = s1.resample( "H" ).ffill()
# print( s1_resample_a )
'''
ffill() 按照前一个值来填充，former fill
bfill() 按照后一个值来填充，back fill
'''

t1 = pd.date_range( "2016-01-01", "2016-12-31", freq = "H" )

df = DataFrame( index = t1 )

df["alibaba"] = np.random.randint( 80, 150, size = len( t1 ) )
df["tencent"] = np.random.randint( 30, 50, size = len( t1 ) )

df.plot()

import matplotlib.pyplot as plt

weekly_df = DataFrame()
weekly_df["alibaba"] = df["alibaba"].resample( "W" ).mean()
weekly_df["tencent"] = df["tencent"].resample( "W" ).mean()

print( weekly_df )

weekly_df.plot()
plt.show()